package com.ruoyi.door.controller.app.baofu;

import cn.hutool.core.util.ObjectUtil;
import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MemberCard;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.door.controller.app.baofu.vo.BaoFuReadSignReqVO;
import com.ruoyi.door.controller.app.baofu.vo.BaoFuUnSignReqVO;
import com.ruoyi.door.controller.app.baofu.vo.SinglePayReqVO;
import com.ruoyi.door.utils.baofu.BaoFuXieYiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/app-api/door/baofu")
@Api(value = "APP 暴富", tags = "APP 暴富")
public class BaoFuController {
    @Autowired
    private BaoFuXieYiService baoFuXieYiService;
    @Autowired
    private EasyEntityQuery easyEntityQuery;

    @PostMapping("/unSign")
    @ApiOperation("解绑卡")
    @Anonymous
    public AjaxResult unSign(@RequestBody BaoFuUnSignReqVO reqVO) throws Exception {

        MemberCard memberCard = easyEntityQuery.queryable(MemberCard.class)
                .where(m -> {
                    m.protocolNo().eq(reqVO.getProtocolNo());
                    m.memberId().eq(SecurityUtils.getUserId());
                }).firstOrNull();
        if (ObjectUtil.isNull(memberCard)) {
            return AjaxResult.error("未找到该协议");
        }
        Map<String, String> stringStringMap = baoFuXieYiService.unSign(reqVO.getProtocolNo());
        if (stringStringMap.get("resp_code").equals("S")) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    @PostMapping("/readySign")
    @ApiOperation("预绑卡")
    @Anonymous
    public AjaxResult readySign(

            @RequestBody BaoFuReadSignReqVO reqVO) throws Exception {
                       /*
            {
    "biz_resp_code": "0000",
    "biz_resp_msg": "交易成功",
    "dgtl_envlp": "a9b2243d3dc26d07422853a7182a485dffc177d37b29a856f21700ba0449c07a76ff9c83199563bbbbab4a1ece009e8631c3468dc3a302ae6d0270766048606d962f0968cd59e3307d5194c4def1ab6d1c3dbf6656839ead3e435a867fe45e2b8f394b48c5f28c3c51b3f373c340b61a25289b2d310333e5f51e81757c75bdbc2191429887963ae2778cc43c35ac7d44603fcb7de2080dd7471601ffb4e501f04f49136665c2afbbc22680c0dbe0e6ad0aa4281154defb99430ab1af25aa3a557023b4210bb0c73123f941f1f9642d75286756b97fe1d6a3e3fbb7fb3d8b6a86f3ecfe1b447f24eadfcab078476c1133e2fa6b2c6681f1bdd0d07dce40f8952a",
    "member_id": "102004459",
    "msg_id": "93f3a2dffeef450aa9b88e608f05a590",
    "resp_code": "S",
    "send_time": "2025-07-01 19:35:12",
    "terminal_id": "100005196",
    "txn_type": "01",
    "unique_code": "dcd46f8f6ff8b82bbee33d2bd598a91a5bf3fbee18734406ad3e3b42a4e5f803",
    "unique_code2": "202507011935123024017",
    "version": "4.0.0.0"
  }
            * */
        reqVO.setUserId(String.valueOf(SecurityUtils.getUserId()));
        return AjaxResult.success(baoFuXieYiService.readySign(reqVO.getCardInfo(), reqVO.getCardType(), reqVO.getUserId()));
    }

    @GetMapping("/confirmSign")
    @ApiOperation("确认绑卡")
    @Anonymous
    public AjaxResult confirmSign(String uniqueCode, String smsCode) throws Exception {
        return AjaxResult.success(baoFuXieYiService.confirmSign(uniqueCode, smsCode));
    }

    @GetMapping("/singlePay")
    @ApiOperation("协议支付-直接支付接口")
    @Anonymous
    public AjaxResult singlePay(SinglePayReqVO reqVO) throws Exception {
        return AjaxResult.success(baoFuXieYiService.singlePay(reqVO
        ));
    }

}
